// exam.js
import request from '@/utils/request'

// 获取考试列表
const getExamList = (params) => {
    // 获取schoolNumber
    const schoolNumber = sessionStorage.getItem('schoolNumber') || localStorage.getItem('schoolNumber')
    
    // 构建请求参数
    const requestParams = {
        schoolNumber: schoolNumber,
        status: params.status || '',
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10,
        searchText: params.searchText || ''
    }
    
    console.log('请求参数:', requestParams) // 添加日志
    
    return request({
        url: '/getExamList',
        method: 'get',
        params: requestParams
    })
}

// 获取考试详情
const getExamDetail = (examId) => {
    return request({
        url: '/getExamDetail',
        method: 'get',
        params: { examId }
    })
}

// 更新考试信息
const updateExam = (examData) => {
    return request({
        url: '/updateExam',
        method: 'put',
        data: examData
    })
}

// 获取老师创建的所有题库
const getQuestionBanks = (teacherId) => {
    return request({
        url: '/questionBanks',
        method: 'get',
        params: { teacherId }
    });
}

// 获取指定题库中的所有试题
const getQuestionsByBank = (questionBankName) => {
    return request({
        url: '/questionsByBank',
        method: 'get',
        params: { questionBankName }
    });
}

// 添加试题到考试
export function addExamQuestions(data) {
    return request({
        url: '/examquestionadd',
        method: 'post',
        data
    });
}

// 从考试中删除试题
const deleteExamQuestion = (data) => {
    return request({
        url: '/exam/question/delete',
        method: 'delete',
        data
    });
}

// 批量删除试卷
const deleteExams = (examIds) => {
    return request({
        url: '/exam/delete',
        method: 'delete',
        data: { examIds }
    });
}

// 获取最近考试列表
export const getRecentExams = () => {
  const schoolNumber = sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')).schoolNumber : null
  return request({
    url: '/recent-exams',
    method: 'get',
    params: { schoolNumber }
  })
}

// 发布考试
const publishExam = (data) => {
    return request({
        url: '/publish',
        method: 'post',
        data
    });
};

// 获取教师创建的所有班级
const getTeacherClasses = (teacherId) => {
    return request({
        url: '/getTeacherClasses',
        method: 'get',
        params: { teacherId }
    });
};

// 获取教师所有班级中的学生
const getTeacherStudents = (teacherId) => {
    return request({
        url: '/getTeacherStudents',
        method: 'get',
        params: { teacherId }
    });
};

// 获取学生考试列表
export function getstudentExamList(schoolNumber) {
  return request({
    url: '/getExamstudent',
    method: 'get',
    params: {
      schoolNumber
    }
  })
}

// 开始考试
export function startExam(examId) {
  return request({
    url: '/startExam',
    method: 'post',
    data: {
      examId
    }
  })
}

// 获取考试题目
export function getExamQuestion(examId) {
  return request({
    url: '/getExamQuestion',
    method: 'get',
    params: {
      examId
    }
  })
}

// 提交答案
export const submitAnswer = (data) => {
    return request({
        url: '/submitAnswer',
        method: 'post',
        data
    });
};

// 获取学生成绩列表
export function getStudentScores(params) {
  return request({
    url: '/getscorelist',
    method: 'get',
    params
  })
}

// 获取成绩详情
export function getScoreDetail(params) {
  return request({
    url: '/getscore',
    method: 'get',
    params
  })
}

// 学生成绩查询
export function studentGradeInquiry(schoolNumber) {
  return request({
    url: '/Studentgradeinquiry',
    method: 'get',
    params: { schoolNumber }
  })
}

// 获取学生已作答的考试
export function getExamstudentAnswer(schoolNumber) {
  return request({
    url: '/getExamstudentAnswer',
    method: 'get',
    params: {
      schoolNumber
    }
  })
}

// 获取主观题答案
export function getSubjectiveAnswers(examId, studentId) {
  return request({
    url: '/getSubjectiveAnswers',
    method: 'get',
    params: {
      examId,
      studentId
    }
  })
}

// 获取考生主观题
export function getSubjectiveQuestion(examId, studentId) {
  return request({
    url: '/getSubjectiveQuestion',
    method: 'get',
    params: {
      examId,
      studentId
    }
  })
}

// 更新考试状态和提交批改结果
export function updateExamStatus(data) {
  return request({
    url: '/updateExamStatus',
    method: 'post',
    data
  })
}

// 更新无主观题考试的状态
export function updateExamStatusNull(examId, studentId) {
  return request({
    url: '/updateExamStatusNUll',
    method: 'post',
    params: {
      examId,
      schoolNumber: studentId
    }
  })
}

// 获取考试成绩列表
export function getExamScoreList(data) {
  return request({
    url: '/getExamScoreList',
    method: 'post',
    data
  })
}

// 获取考试成绩详情
export function getExamScoreDetail(examId) {
  return request({
    url: '/getExamScoreDetail',
    method: 'get',
    params: { examId }
  })
}

// 获取教师班级列表（新接口）
export function getClassID(teacherId) {
    return request({
        url: '/getClassID',
        method: 'get',
        params: { teacherId }
    });
}

// 根据班级ID获取试卷列表
export function getExamByClassID(classID) {
    return request({
        url: '/getExam',
        method: 'get',
        params: { classID }
    });
}

// 获取饼形图信息
export function getPieChart(classID, examId) {
    return request({
        url: '/getPieChart',
        method: 'get',
        params: { classID, examId }
    });
}

// 学生成绩详情查询
export function studentResultsForDetails(examId, studentId) {
  return request({
    url: '/Studentresultsfordetails',
    method: 'get',
    params: { examId, studentId }
  })
}

// 获取错题本
export function getMistakeCollection(params) {
  return request({
    url: '/getMistakeCollection',
    method: 'get',
    params
  })
}

// 更新错题状态
export function updateMistakeStatus(mistake_id, status) {
  return request({
    url: `/updateMistakeStatus/${mistake_id}/${status}`,
    method: 'get'
  })
}

// 获取学生所属班级信息
export function getStudentClassInfo(studentID) {
  return request({
    url: '/getStudentClassInfo',
    method: 'get',
    params: { studentID }
  })
}

// 记录作弊行为
export function recordCheating(data) {
  return request({
    url: '/exam/cheating',
    method: 'post',
    data
  })
}

export default {
    getExamList,
    getExamDetail,
    updateExam,
    getQuestionBanks,
    getQuestionsByBank,
    addExamQuestions,
    deleteExamQuestion,
    deleteExams,
    getRecentExams,
    publishExam,
    getTeacherClasses,
    getTeacherStudents,
    getExamQuestion,
    submitAnswer,
    getStudentScores,
    getScoreDetail,
    getExamstudentAnswer,
    getSubjectiveAnswers,
    getSubjectiveQuestion,
    updateExamStatus,
    updateExamStatusNull,
    getExamScoreList,
    getExamScoreDetail,
    getClassID,
    getExamByClassID,
    getPieChart,
    studentGradeInquiry,
    studentResultsForDetails,
    getMistakeCollection,
    updateMistakeStatus,
    getStudentClassInfo,
    recordCheating
}